package cn.maszy.action.systemsetting;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import org.apache.struts2.ServletActionContext;
import cn.maszy.common.BaseAction;
import cn.maszy.model.systemsetting.B_T_Ufp_Project;


public class UploadAction extends BaseAction<B_T_Ufp_Project>{
	private static final long serialVersionUID = 1L;
	private static final int BUFFER_SIZE = 1024 * 1024;
	private File myFile;// 上传的图片
	private String fileName;// 图片上传时的名字
	private String imageFileName;// 给图片随机产生的名字，方便与存放
	private String caption;// 显示图片时自己输入的名字
	private File imageFile;

	public void copy(File src, File dst) {
		try {
			InputStream in = null;
			OutputStream out = null;
			try {
				in = new BufferedInputStream(new FileInputStream(src),BUFFER_SIZE);
				out = new BufferedOutputStream(new FileOutputStream(dst),BUFFER_SIZE);
				byte[] buffer = new byte[BUFFER_SIZE];
				byte[] buf = new byte[in.available()];

				if (in.available() < BUFFER_SIZE && in.available() > 0) {
					in.read(buf);
					out.write(buf);
				}
				while (in.available() >= BUFFER_SIZE) {
					in.read(buffer);
					out.write(buffer);
					if (in.available() < BUFFER_SIZE) {
						byte[] buff = new byte[in.available()];
						in.read(buff);
						out.write(buff);
					}
				}

			} finally {
				if (null != in) {
					in.close();
				}
				if (null != out) {
					out.close();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 截取图片名字（.后的那部分）
	 * 
	 * @param fileName
	 * @return
	 */
	public static String getExtention(String fileName) {
		int pos = fileName.lastIndexOf(".");
		return fileName.substring(pos);
	}

	public String upload() {
//		 imageFileName = new Date().getTime() + getExtention(fileName);
		 String file = ServletActionContext.getServletContext().getRealPath(
		 "/zy-admin/Download");
		 if(fileName==null ||myFile==null)
		 {
			 return ajaxForwardError("请先选择文件");
		 }
		 imageFile = new File(file + "/" + fileName);// 图片存放的地址
		 copy(myFile, imageFile);// 一切都设置好后，就将图片放在imageFile下
		 return ajaxForwardSuccess(getText("上传成功"), "","", "", "");
		}
	
		public void main(String[] args) {
		readFileByLines(myFile);
		}


	/**
	 * 以行为单位读取文件，常用于读面向行的格式化文件
	 */
	public static void readFileByLines(File fileName) {
		// File file = new File(fileName);
		BufferedReader reader = null;
		try {
//			System.out.println("以行为单位读取文件内容，一次读一整行：");
			reader = new BufferedReader(new FileReader(fileName));
			String tempString = null;
			
			// 一次读入一行，直到读入null为文件结束
			while ((tempString = reader.readLine()) != null) {
				if (tempString.length() != 0) {

					// 显示行号
					String[] str = tempString.split(",");
					for (String st : str) {
						System.out.println("===============" + st);
					}
				}
			}
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e1) {
				}
			}
		}
	}

	public void setMyFileFileName(String fileName) {
		this.fileName = fileName;
	}

	public void setMyFile(File myFile) {
		this.myFile = myFile;
	}

	public String getImageFileName() {
		return imageFileName;
	}

	public String getCaption() {
		return caption;
	}

	public void setCaption(String caption) {
		this.caption = caption;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public File getImageFile() {
		return imageFile;
	}

	public void setImageFile(File imageFile) {
		this.imageFile = imageFile;
	}
	public File getMyFile() {
		return myFile;
	}

	public void setImageFileName(String imageFileName) {
		this.imageFileName = imageFileName;
	}

}